package demo4.mapper;

import demo4.pojo.User;
import demo4.util.IDBUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@Component
public class UserMapper implements IUserMapper{
    @Autowired
    private IDBUtil dbUtil;
    @Override
    public int insertUser(User user) {
        System.out.println("UserMapper将用户信息插入到数据库中-------");

        try (Connection connection = dbUtil.getConnection())
        {
            String sql = "INSERT INTO user(username , password , nickname , age ) VALUES(?, ?, ?, ?)";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, user.getUsername());
            preparedStatement.setString(2, user.getPassword());
            preparedStatement.setString(3, user.getNickname());
            preparedStatement.setInt(4, user.getAge());
            return preparedStatement.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public User selectbyusernameUser(String username) {
        try (Connection connection = dbUtil.getConnection()) {
            String sql = "SELECT username , password , nickname , age FROM user WHERE username = ?";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);// 创建PreparedStatement对象
            preparedStatement.setString(1, username);
            ResultSet resultSet = preparedStatement.executeQuery();// 执行查询
            if (resultSet.next()) {
                User user = new User();
                user.setUsername(resultSet.getString("username"));
                user.setPassword(resultSet.getString("password"));
                user.setNickname(resultSet.getString("nickname"));
                user.setAge(resultSet.getInt("age"));
                return user; // 用户存在
            }
        }catch (SQLException e){
            throw new RuntimeException(e);
        }
        return null;
    }

    @Override
    public List<User> selectAllUser() {
        List<User> list = new ArrayList<>();
        try (Connection connection = dbUtil.getConnection()) {
            String sql = "SELECT id , username , password , nickname , age FROM user";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                User user = new User();
                user.setId(resultSet.getInt("id"));
                user.setUsername(resultSet.getString("username"));
                user.setPassword(resultSet.getString("password"));
                user.setNickname(resultSet.getString("nickname"));
                user.setAge(resultSet.getInt("age"));
                list.add(user);
            }
        }catch (SQLException e){
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public int updateUser(User user) {
        try (Connection connection = dbUtil.getConnection()) {
            String sql = "UPDATE user SET password = ? WHERE username = ?";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, user.getPassword());
            preparedStatement.setString(2, user.getUsername());
            return preparedStatement.executeUpdate();
        }catch (SQLException e){
            e.printStackTrace();
        }
        return 0;
    }
}